import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutterdemo/0demo/bloc/bloc/counter_bloc.dart';
import 'package:flutterdemo/0demo/bloc/cubit/counter_view.dart';
import 'package:flutterdemo/0demo/bloc/multi_state_bloc/multi_state_bloc_test_page.dart';

class CounterPage extends StatelessWidget {
  const CounterPage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // page wrapped by BlocProvider
    return BlocProvider(
      create: (BuildContext context) => CounterBloc(),
      child: Scaffold(
        appBar: AppBar(
          title: Text("bloc test"),
          actions: [
            GestureDetector(
              child: Center(child: Text("multi state bloc")),
              onTap: () {
                Navigator.push(
                    context,
                    CupertinoPageRoute(
                        builder: (context) => MultiStateBlocTestPage()));
              },
            ),
            SizedBox(width: 20),
            GestureDetector(
              child: Center(child: Text("cubit test")),
              onTap: () {
                Navigator.push(context,
                    CupertinoPageRoute(builder: (context) => CounterView()));
              },
            ),
            SizedBox(width: 20),
          ],
        ),
        body: Stack(
          children: [
            Align(
              alignment: Alignment.topCenter,
              child: Container(
                margin: EdgeInsets.all(10),
                child: BlocBuilder<CounterBloc, CounterState>(
                  buildWhen: (pState, cState) {
                    // 1. 可以更加细粒度控制子widget build的时机
                    // 2. 可以选择在什么state做什么事情，比如刷新完的state，下拉刷新关闭动画
                    return pState.title != cState.title;
                  },
                  builder: (context, state) {
                    print("title build");
                    return GestureDetector(
                      // state data used here.
                      child: Text(state.title),
                      onTap: () {
                        // send change title event
                        BlocProvider.of<CounterBloc>(context)
                            .add(ChangeTitleEvent(title: state.title + "aa"));
                      },
                    );
                  },
                ),
              ),
            ),
            Center(
              child: BlocBuilder<CounterBloc, CounterState>(
                buildWhen: (pState, state) {
                  print("${pState.count}, ${state.count}");
                  return pState.count != state.count;
                },
                builder: (context, state) {
                  print("count build");
                  return Text(state.count.toString());
                },
              ),
            ),
            Align(
              alignment: Alignment.bottomRight,
              child: Container(
                padding: EdgeInsets.all(50),
                child: BlocBuilder<CounterBloc, CounterState>(
                  builder: (context, state) {
                    return FloatingActionButton(
                      onPressed: () {
                        // send add count event
                        BlocProvider.of<CounterBloc>(context)
                            .add(AddCountEvent());
                      },
                      child: Icon(Icons.add),
                    );
                  },
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
